import 'dart:async';
import 'dart:convert';

import 'package:flutter/widgets.dart';
import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:love_newf/app/com/manage/txim_manage/im_manage.dart';
import 'package:love_newf/app/com/model/login_user_info_model.dart';
import 'package:love_newf/app/com/base/key_mix.dart';
import 'package:love_newf/app/page/home/home_page.dart';
import 'package:love_newf/generated/l10n.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/util/toast_util.dart';
import 'package:love_newf/app/com/http/http_request.dart';
import 'package:love_newf/app/com/http/Res.dart';
import 'package:love_newf/app/com/service/user_service.dart';

class LoginLogic extends GetxController {
  String phone = "", password = "", rePassword = "", code = ""; //默认 202209
  var codeCount = 60.obs;
  UserService userService = UserService();
  bool agree = false;
  bool isBack = true;
  FocusNode codeFocusNode = FocusNode();

  //验证吗登录
  bool _isCodeLogin = false;

  get isCodeLogin => _isCodeLogin;

  @override
  onInit() {
    try {
      isBack = Get.arguments['isBack'];
    } catch (e) {}
  }

  get isVerPhone {
    if (code.length == 11) {
      ToastUtils.toast("手机号位数不对！");
      return false;
    }
    return true;
  }

  set isCodeLogin(b) {
    _isCodeLogin = b;
    update();
  }

  Timer? _timer;

  login() async {
    if (!agree) {
      ToastUtils.toast("请先同意隐私政策！");
      return;
    }

    if (!isVerPhone) return;

    if (code.isEmpty) {
      ToastUtils.toast("验证码不能为空！");
      return null;
    }
    dynamic data = await HttpRequest().post<LoginUserInfoModel>(
        "/auth/login/code", {"phone": phone, "code": code},
        loadingText: S.current.loading);
    if (data == null) return null;
    saveInfo(data);
    if (await ImManage().login()) {
      Get.offAll(HomePage());
    }
  }

  saveInfo(LoginUserInfoModel info) {
    SpUtils().setStorage("loginUserInfo", json.encode(info.toJson()));
  }

  getCode() async {
    if (!isVerPhone) return;

    if (codeCount.value == 60) {
      Res r = await HttpRequest().post('/auth/sms/send', {'phone'.hx: phone},
          loadingText: S.current.loading, parse: false);
      if (r.success) {
        codeCount--;
        _timer = Timer.periodic(const Duration(milliseconds: 1000), (timer) {
          if (codeCount.value == 1) {
            codeCount.value = 60;
            _timer?.cancel();
            return;
          }
          codeCount--;
        });
        codeFocusNode.requestFocus();
      } else {
        ToastUtils.toast(r.msg);
      }
    }
  }

  // Future<bool> safetyVerity(phone) async {
  //   Res s = await HttpRequest().postRes(
  //       '/service/login/if-check'.hx, {'username'.hx: phone},
  //       loadingText: S.current.loading);
  //   if (s.data?["check_auth"] == 1) {
  //     await Get.to(const SafetyVerifyPage(), arguments: {"phone": phone});
  //     return true;
  //   }
  //   return false;
  // }

  @override
  void onClose() {
    _timer?.cancel();
    super.onClose();
  }
}
